package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.utils.RegluarUtils;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

public class ActivityPasswordActivity extends BaseActivity {
    private EditText etName, etCode, etQrcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_password_send);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("修改登录密码");
    }

    @Override
    public void initView() {
        super.initView();
        etName = findViewById(R.id.et_name);
        etCode = findViewById(R.id.et_code);
        etQrcode = findViewById(R.id.et_qrcode);
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.bt_pwd).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_pwd:
                String name = etName.getText().toString();
                String code = etCode.getText().toString();
                String qrcode = etQrcode.getText().toString();
                if (StringUtils.isEmpty(name)) {
                    showToast("旧密码不能为空");
                    return;
                }
                if (StringUtils.isEmpty(code)) {
                    showToast("新密码，不能为空");
                    return;
                }
                if (!RegluarUtils.isLetterDigit(code)) {
                    showToast("新密码长度必须8~16位，并且包含字母和数字");
                    return;
                }
                if (!code.equals(qrcode)) {
                    showToast("新密码不一致");
                    return;
                }
                Users users = new Users();
                users.setPassword(code);
                users.setBuRsaKey(name);
                users.setToken(pageVo.getToken());
                getClick(users);
                break;
        }
    }

    private void getClick(Users users) {
        loading();
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.OLDPWD_URL,
                hucParameInterfase.getRequestParameter(users), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                String msg = (String) response;
                if ("保存成功".equals(msg)) {
//                    setSharedPreferences("tokenRSA", "token001");
//                    DataUtils.deleteToken(this);
//                    sendIntent(this, LoginActivity.class);
                    showToast("保存成功，请重新登录");
                    finish();
                } else {
                    showToast(msg);
                }
                break;
        }
        loadDismiss();
    }
}
